<template>
  <div :style="{ background: `url(${bg})`, backgroundSize: 'cover', backgroundRepeat: 'no-repeat' }" class="box">
    <div style="color: white;" class="box-left">
      <div class="box-left-card">
        <section>
          <div>累计确诊人数</div>
          <div>{{ store.totalConfirmed }}</div>
        </section>
        <section>
          <div>现有确诊人数</div>
          <div>{{ store.currentConfirmed }}</div>
        </section>
        <section>
          <div>无症状感染者</div>
          <div>{{ store.asympTomatic }}</div>
        </section>
        <section>
          <div>新增确诊人数</div>
          <div>{{ store.newConfirmed }}</div>
        </section>
        <section>
          <div>新增治愈人数</div>
          <div>{{ store.newRecovered }}</div>
        </section>
        <section>
          <div>新增死亡人数</div>
          <div>{{ store.newDeaths }}</div>
        </section>
      </div>
      <div class="box-left-pie"></div>
      <div class="box-left-line"></div>
    </div>


    <div id="china" class="box-center">
    </div>

    <div style="color: white;" class="box-right">
      <table class="table" cellspacing="0" border="1px">
        <thead>
          <tr>
            <th>城市</th>
            <th>累计确诊</th>
            <th>新增确诊</th>
            <th>治愈</th>
            <th>死亡</th>
          </tr>
        </thead>
        <transition-group enter-active-class="animate__animated animate__flipInY" tag="tbody">
          <tr :key="index" v-for="(item, index) in store.item">
            <td align="center"> {{ item.city }}</td>
            <td align="center">{{ item.confirmed }}</td>
            <td align="center">{{ item.new_confirmed }}</td>
            <td align="center">{{ item.recovered }}</td>
            <td align="center">{{ item.deaths }}</td>
          </tr>
        </transition-group>
      </table>
    </div>
  </div>
</template>

<script setup lang="ts">
import bg from './assets/1.png'
import { useStore } from './stores'
import { onMounted, ref, computed, reactive } from 'vue'
import * as echarts from 'echarts' //echarts 5
import './assets/china'
import { geoCoordMap } from './assets/geoMap'
import 'animate.css'

const store = useStore()
onMounted(async () => {
  await store.getList()
  initCharts()
  initPie()
  initLine()
})

const initCharts = () => {
  const city = store.list.data;
  store.item = city[18].cities
  const data = city.map(v => {
    return {
      name: v.province,
      value: geoCoordMap[v.province],
      cities: v.cities,
      selected: v.province == "广东"
    }
  })

  const charts = echarts.init(document.querySelector('#china') as HTMLElement)
  charts.setOption({
    // backgroundColor: "black",
    geo: {
      map: "china",
      aspectScale: 0.8,
      layoutCenter: ["50%", "50%"],
      layoutSize: "100%",
      itemStyle: {
        // normal: {
        areaColor: {
          type: "linear-gradient",
          x: 0,
          y: 1200,
          x2: 1000,
          y2: 0,
          colorStops: [
            {
              offset: 0,
              color: "#152E6E", // 0% 处的颜色
            },
            {
              offset: 1,
              color: "#0673AD", // 50% 处的颜色
            },
          ],
          global: true, // 缺省为 false
        },
        shadowColor: "#0f5d9d",
        shadowOffsetX: 0,
        shadowOffsetY: 15,
        opacity: 0.5,
        // },

      },
      emphasis: {
        areaColor: "#0f5d9d",
      },

      regions: [
        {
          name: "南海诸岛",
          itemStyle: {
            areaColor: "rgba(0, 10, 52, 1)",
            borderColor: "rgba(0, 10, 52, 1)",
            // normal: {
            opacity: 0,
            label: {
              show: false,
              color: "#009cc9",
            },
            // },
          },
          label: {
            show: false,
            color: "#FFFFFF",
            fontSize: 12,
          },
        },
      ],
    },
    series: [
      {
        type: "map",
        // selectedMode: "multiple",
        map: "china",
        aspectScale: 0.8,
        layoutCenter: ["50%", "50%"], //地图位置
        layoutSize: "100%",
        // zoom: 1, //当前视角的缩放比例
        // roam: true, //是否开启平游或缩放
        // scaleLimit: {
        //滚轮缩放的极限控制
        // min: 1,
        // max: 2,
        // },
        label: {
          show: true,
          color: "#FFFFFF",
          fontSize: 12,
        },
        itemStyle: {
          // normal: {
          areaColor: "#0c3653",
          borderColor: "#1cccff",
          borderWidth: 1.8,
          // },

        },
        emphasis: {
          areaColor: "#56b1da",
          label: {
            show: true,
            color: "#fff",
          },
        },
        data: data,
      },
      {
        type: 'scatter',
        coordinateSystem: 'geo',
        //   symbol: 'image://http://ssq168.shupf.cn/data/biaoji.png',
        // symbolSize: [30,120],
        // symbolOffset:[0, '-40%'] ,
        symbol: 'pin',
        symbolSize: [45, 45],
        label: {
          // normal: {
          show: true,
          color: "#fff",
          formatter(value: any) {
            return value.data.value[2]
          }
          // }
        },
        itemStyle: {
          // normal: {
          color: '#1E90FF', //标志颜色
          // }
        },
        //展示每个省的数据
        // data: data,
      },
    ],
  })

  charts.on('click', (e: any) => {
    console.log('e', e.data)
    store.item = e.data.cities
  })
}

const initPie = () => {
  const charts = echarts.init(document.querySelector('.box-left-pie') as HTMLElement)
  charts.setOption({
    backgroundColor: "#223651",
    tooltip: {
      trigger: 'item'
    },
    title: {
      text: '全国现有确诊人数排行', // 标题文本
      left: 'center',
      top: 'top',
      textStyle: {
        fontSize: 14,       // 字体大小
        fontWeight: 'bold', // 字体粗细
        color: '#FFF'       // 字体颜色
      }
    },
    series: [
      {
        type: 'pie',
        radius: ['40%', '70%'],
        itemStyle: {
          borderRadius: 4,
          borderColor: '#fff',
          borderWidth: 2
        },
        label: {
          show: true,
        },
        emphasis: {
          label: {
            show: true,
            fontSize: '15',
          }
        },
        data: store.cityDetail.map(v => {
          return {
            name: v.city,
            value: v.current_confirmed
          }
        })
      }
    ]
  })
}

const initLine = () => {
  const charts = echarts.init(document.querySelector('.box-left-line') as HTMLElement)
  charts.setOption({
    backgroundColor: "#223651",
    // 标题
    title: {
      text: '各省确诊人数排行',
      left: 'center',
      textStyle: {
        fontSize: 14,       // 字体大小
        fontWeight: 'bold', // 字体粗细
        color: '#FFF'       // 字体颜色
      }
    },
    // X 轴
    xAxis: {
      type: 'category', // 类目轴
      data: store.cofirmRank.map(v => v.province),
      axisLabel: {
          color: '#fff',
          fontSize: 10
        }
    },
    // Y 轴
    yAxis: {
      type: 'value', // 数值轴
      axisLabel: {
          color: '#fff',
          fontSize: 8
        }
    },
    // 数据系列
    series: [
      {
        name: '人数',
        type: 'bar', // 柱状图类型
        data: store.cofirmRank.map(v => v.total_confirmed),
        label: {
          show: false,
        },
        emphasis: {
          label: {
            show: true,
            fontSize: '12',
          }
        },
      }
    ]
  })
}

// const initLine = () => {
//   const charts = echarts.init(document.querySelector('.box-left-line') as HTMLElement)
//   charts.setOption({
//     backgroundColor: "#223651",
//     tooltip: {
//       trigger: 'axis'
//     },
//     xAxis: {
//       type: 'category',
//       data: store.cityDetail.map(v => v.city),
//       axisLine: {
//         lineStyle: {
//           color: "#fff"
//         }
//       }
//     },
//     yAxis: {
//       type: 'value',
//       axisLine: {
//         lineStyle: {
//           color: "#fff"
//         }
//       }
//     },
//     label: {
//       show: true
//     },
//     series: [
//       {
//         data: store.cityDetail.map(v => v.current_confirmed),
//         type: 'line',
//         smooth: true
//       }
//     ]
//   })
// }

</script>

<style lang="less">
* {
  margin: 0;
  padding: 0;
}

@itemColor: #41b0db;
@itemBg: #223651;
@itemBorder: #f3f2f7;

.table {
  width: 100%;
  background: #9c95c3;
  border: 1px solid @itemBorder;

  tr {
    th {
      padding: 5px;
      white-space: nowrap;
    }

    td {
      padding: 5px 10px;
      width: 100px;
      white-space: nowrap;
    }
  }
}

html,
body,
#app {
  height: 100%;
  overflow: hidden;
}

.box {
  height: 100%;
  display: flex;
  overflow: hidden;
  padding: 10px;

  &-left {
    width: 400px;

    &-pie {
      height: 300px;
      margin-top: 20px;
    }

    &-line {
      height: 220px;
      margin-top: 15px;
    }

    &-card {
      display: grid;
      grid-template-columns: auto auto auto;
      grid-template-rows: auto auto;

      section {
        background: @itemBg;
        border: 1px solid @itemBorder;
        padding: 8px;
        display: flex;
        flex-direction: column;
        align-items: center;

        div:nth-child(2) {
          color: @itemColor;
          padding: 6px 0;
          font-size: 16px;
          font-weight: bold;
        }
      }
    }
  }

  &-center {
    flex: 1;
  }

  &-right {
    width: 400px;
    // background: green;
  }
}
</style>